package com.modifiertech.lineage.network;

import com.modifiertech.lineage.BaseConst;
import com.modifiertech.lineage.LineageRegistries;
import com.modifiertech.lineage.api.PlayerData;
import com.modifiertech.lineage.event.PlayerDataHandler;
import com.modifiertech.lineage.handler.SkillAnimateCallHandler;
import com.modifiertech.lineage.handler.SkillLevelUpHandler;
import com.modifiertech.lineage.handler.SkillShortCutBindHandler;
import io.netty.buffer.ByteBuf;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.network.ByteBufUtils;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

/**客户端到服务端之间的
 *
 *
 * ClassName: MessageSkillModify <br/>
 * Description: <br/>
 * date: 2020/8/28 21:43<br/>
 *
 * @author Administrator<br />
 * @since JDK 1.8
 */
public class MessageAnimateCall implements IMessage, IMessageHandler<MessageAnimateCall,IMessage> {
    private NBTTagCompound nbtTagCompound=new NBTTagCompound();

    @Override
    public void fromBytes(ByteBuf byteBuf) {
        nbtTagCompound= ByteBufUtils.readTag(byteBuf);
    }

    @Override
    public void toBytes(ByteBuf byteBuf) {
        ByteBufUtils.writeTag(byteBuf,nbtTagCompound);
    }

    @Override
    public IMessage onMessage(MessageAnimateCall messageAnimateCall, MessageContext messageContext) {
        FMLCommonHandler.instance().getMinecraftServerInstance().addScheduledTask(()->{
            handleMessage(messageAnimateCall,messageContext);
        });
        return null;
    }
    public void handleMessage(MessageAnimateCall messageAnimateCall, MessageContext messageContext){
        String skillName=
                messageAnimateCall.getNbtTagCompound().getString("skillName");
        Boolean skillEnhance =
                messageAnimateCall.getNbtTagCompound().getBoolean("skillEnhance");

        SkillAnimateCallHandler.INSTANCE.skillAnimateCall(messageContext.getServerHandler().player
                ,skillName,skillEnhance);
    }

    public NBTTagCompound getNbtTagCompound() {
        return nbtTagCompound;
    }

    public void setNbtTagCompound(NBTTagCompound nbtTagCompound) {
        this.nbtTagCompound = nbtTagCompound;
    }
    //构造技能动画回调NBT
    public synchronized MessageAnimateCall write(String skillName,boolean enhance){
        ResourceLocation skillResource=new ResourceLocation(BaseConst.MODID,skillName);
        if(!LineageRegistries.SKILLS.containsKey(skillResource))return this;
        //INIT
        this.getNbtTagCompound().setString("skillName",skillName);
        this.getNbtTagCompound().setBoolean("skillEnhance",enhance);
        return this;
    }

}
